/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            productManageInfo: {
                products: [],
                total: 0,
                records: 1,
                moreCondition: false,
                productId: '',
                states: [],
                conditions: {
                    categoryId: '',
                    state: '',
                    prodName: '',
                    keyword: '',
                    barCode: '',
                    shopId: '',
                    shId:'',
                    hktId:''
                },
                productCategorys: [{
                    categoryId:'',
                    categoryName:'全部'
                }],
                shopHouses: [],
                housekeepingTypes: [],
            }
        },
        _initMethod: function () {
            $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            $that._listHousekeepingTypes();
            $that.productManageInfo.conditions.shopId = vc.getCurrentCommunity().shopId;
            vc.getDict('product', "state", function (_data) {
                $that.productManageInfo.states = _data;
            });
            $that._listProductCategorys();
            $that._listShopHouses();
        },
        _initEvent: function () {
            vc.on('productManage', 'choose', function (_param) {
                $that._doEnterProductStock(_param.productId, _param.valueId);
            });
            vc.on('productManage', 'listProduct', function (_param) {
                $that.productManageInfo.componentShow = 'productManage';
                $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('pagination', 'page_event', function (_currentPage) {
                $that._listProducts(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _listProducts: function (_page, _rows) {
                $that.productManageInfo.conditions.page = _page;
                $that.productManageInfo.conditions.row = _rows;
                $that.productManageInfo.conditions.shopId = vc.getCurrentCommunity().shopId;
                let param = {
                    params: $that.productManageInfo.conditions
                };
                param.params.prodName = param.params.prodName.trim();
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.productManageInfo.total = _json.total;
                        $that.productManageInfo.records = _json.records;
                        $that.productManageInfo.products = _json.data;
                        //$that.addProductInfo.sort = $that.productManageInfo.products.length;
                        vc.emit('pagination', 'init', {
                            total: $that.productManageInfo.records,
                            dataCount: $that.productManageInfo.total,
                            currentPage: _page
                        });
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listProductCategorys: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                let _categorys = [{
                    categoryId:'',
                    categoryName:'全部'
                }];
                //发送get请求
                vc.http.apiGet('/productCategory.queryProductCategory',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        _json.data.forEach(c=>{
                            _categorys.push(c);
                        })
                        $that.productManageInfo.productCategorys = _categorys;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _openAddProductModal: function () {
                vc.jumpToPage('/#/pages/goods/addProduct?categoryId='+$that.productManageInfo.conditions.categoryId)
            },
            _openEditProductModel: function (_product) {
                vc.jumpToPage('/#/pages/goods/editProduct?productId='+_product.productId)

            },
            _openDeleteProductModel: function (_product) {
                vc.emit('deleteProduct', 'openDeleteProductModal', _product);
            },
            _openProductDetail:function(_product){
                window.open('/#/pages/goods/productDetail?productId='+_product.productId)
            },
            //查询
            _queryProductMethod: function () {
                $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetProductMethod: function () {
                $that.productManageInfo.conditions.prodName = "";
                $that.productManageInfo.conditions.categoryId = "";
                $that.productManageInfo.conditions.state = "";
                $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _doEnterProductStock: function (_productId, _valueId) {
                vc.jumpToPage('/#/pages/goods/productStockManage?productId=' + _productId + "&valueId=" + _valueId)
            },
            _moreCondition: function () {
                if ($that.productManageInfo.moreCondition) {
                    $that.productManageInfo.moreCondition = false;
                } else {
                    $that.productManageInfo.moreCondition = true;
                }
            },
            _listShopHouses: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopHouse.queryShopHouse',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.productManageInfo.shopHouses = _json.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listHousekeepingTypes: function (_page, _rows) {
                let param = {
                    params: {
                        page: 1,
                        row: 30,
                        typeCd: '2002',
                        shopId: '9999',
                        skipType: '3'
                    }
                };
                //发送get请求
                vc.http.apiGet('/housekeepingType.queryHousekeepingType',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        $that.productManageInfo.housekeepingTypes = _json.data;

                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _openProductLabelModel: function (_product) {
                vc.emit('addProductLabel', 'openAddLabelModal', _product);
            },
            _viewProductQrcode:function(_product){
                vc.emit('viewQrCode', 'openQrCodeModal', {
                    title: _product.prodName,
                    url: _product.pageUrl,
                    remark: '手机扫码查看商品'+_product.pageUrl,
                });
            },
            swatchCategory:function(_catalog){
                $that.productManageInfo.conditions.categoryId = _catalog.categoryId;
                $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _exportProduct: function(){
                $that.productManageInfo.conditions.pagePath = "storeProductManage";
                let param = {
                    params: $that.productManageInfo.conditions
                };
                vc.http.apiGet("/export.exportData", param, function (json, res){
                        let _json = JSON.parse(json);
                        vc.toast(_json.msg);
                        if (_json.code == 0){
                            vc.jumpToPage('/#/pages/property/downloadTempFile?tab=下载中心&fileType=storeProductManage')
                        }
                    },
                    function (errInfo, error){
                        console.log('请求失败处理');
                    }

                );
            }
        }
    });
})(window.vc);
